/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cn.kcrx.autoweightapp.activity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.viewbinding.ViewBinding
import cn.kcrx.autoweightapp.core.BaseActivity
import cn.kcrx.autoweightapp.fragment.MainFragment
import com.xuexiang.xaop.annotation.DebugLog
import com.xuexiang.xaop.annotation.Permission
import com.xuexiang.xaop.annotation.SingleClick
import com.xuexiang.xaop.consts.PermissionConsts
import dagger.hilt.android.AndroidEntryPoint
import mu.KotlinLogging

/**
 * 程序入口，空壳容器
 *
 * @author xuexiang
 * @since 2019-07-07 23:53
 */
@AndroidEntryPoint
class MainActivity : BaseActivity<ViewBinding?>() {
    private val logger=KotlinLogging.logger {}

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        logger.info { "主页已经打开..." }
        openPage(MainFragment::class.java)
        checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE).let {
            if(it== PackageManager.PERMISSION_GRANTED){
                logger.error { "permisson WRITE_EXTERNAL_STORAGE is PERMISSION_GRANTED" }
            }else{
                //无授权准备开始申请
                logger.error{"permisson is ${it}"}
                requestStorePermisson()
            }
        }
    }




    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if(requestCode==0){
            logger.error { "grantResults=${grantResults.joinToString(",")}" }
            if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               logger.error { "Permission WRITE_EXTERNAL_STORAGE is PERMISSION_GRANTED OK" }
            }else{
                logger.error { "Permission WRITE_EXTERNAL_STORAGE is PERMISSION_GRANTED denied!!!" }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
    private fun requestStorePermisson() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE),0)
        }
    }
}